package cc.bailongma.me;

import java.util.ArrayList;
import java.util.List;


import cc.bailongma.me.fragment.GuideItemFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;


public class GuideActivity extends FragmentActivity implements OnPageChangeListener{

	private ViewPager viewPager;
	private int imgs[]={
			R.drawable.b01,
			R.drawable.b02,
			R.drawable.b03,
			R.drawable.b04
	};
	private boolean isScroll;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		setViewPager();
	}
	private List<GuideItemFragment> frgList;
	private void setViewPager(){
		frgList=new ArrayList<GuideItemFragment>();
		for(int i=0;i<imgs.length;i++){
			GuideItemFragment frg=
			new GuideItemFragment();
			frg.setImageId(imgs[i]);
			frgList.add(frg);
		}
		viewPager=
		(ViewPager)findViewById(R.id.guidePagerId);
		GuidePagerAdapter adapter=
		new GuidePagerAdapter(
	    getSupportFragmentManager());
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(this);
		
	}
	class GuidePagerAdapter extends 
	        FragmentPagerAdapter{
		public GuidePagerAdapter(FragmentManager fm) {
			super(fm);
		}
		@Override
		public int getCount() {
			return frgList.size();
		}
		@Override
		public Fragment getItem(int pos) {
			return frgList.get(pos);
		}
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}
	@Override
	public void onPageScrollStateChanged(int arg0) {
		switch (arg0) {
		case ViewPager.SCROLL_STATE_DRAGGING:
			isScroll=false;
			break;
		case ViewPager.SCROLL_STATE_SETTLING:
			isScroll=true;
			break;		
		case ViewPager.SCROLL_STATE_IDLE:
			if (viewPager.getCurrentItem() == viewPager.getAdapter().getCount() - 1 && !isScroll) {
				Intent intent=new Intent(this,LoginActivity.class);
				startActivity(intent);
				finish();
			}
			isScroll = true;
			break;
		}
	}
	@Override
	public void onPageSelected(int pos) {
		
	}
	public void onClick(View view){
		Intent intent=new Intent();
		intent.setClass(this,MainActivity.class);
		startActivity(intent);
		finish();
	}

}
